from mapper.user_mapper import UserMapper
from model.User_model import UserModel


class UserService:
    """
        测试用例服务层
    """

    @classmethod
    def get(cls, user_id):
        return UserMapper.get(user_id)

    @classmethod
    def get_name(cls, username):
        return UserMapper.get_name(username)

    @classmethod
    def save(cls, user_entity: UserModel):
        """
        1、查询用例，是否存在，如果存在，则不新增
        2、反之就做新增操作
        :return:
        """
        if cls.get(user_entity.id):
            return False
        else:
            UserMapper.save(user_entity)
            return True

    @classmethod
    def delete(cls, user_id):
        if cls.get(user_id):
            UserMapper.delete(user_id)
            return True
        else:
            return False

    @classmethod
    def get_all(cls):
        return UserMapper.get_all()

    @classmethod
    def update(cls, update_user: dict):
        user_id = update_user.get("id")
        if cls.get(user_id):
            UserMapper.update(user_id, update_user)
            return True
        else:
            return False
